/*
 * @Author: yc
 * @Date: 2020-05-26 20:49:07
 * @LastEditTime: 2020-05-26 21:10:42
 * @FilePath: \server\index.js
 */

const mongoose = require('mongoose')
const express = require('express')
const cookieParser = require('cookie-parser')
const userRouter = require('./routes/user')

const app = express() // 产生应用对象


app.use(express.static('public'))
app.use(express.urlencoded({extended: true})) // 请求体参数是: name=tom&pwd=123
app.use(express.json()) // 请求体参数是json结构: {name: tom, pwd: 123}
app.use(cookieParser())

app.use('/', userRouter)

mongoose.connect('mongodb://localhost/yc', {useNewUrlParser: true})
  .then(() => {
    console.log('连接数据库成功!!!')
    // 只有当连接上数据库后才去启动服务器
    app.listen('5000', () => {
      console.log('服务器启动成功, 请访问: http://localhost:5000')
    })
  })
  .catch(error => {
    console.error('连接数据库失败', error)
  })

